package array;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author <a href="zhaoyingling12@163.com">Zhao Simon</a>
 * @description: https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/ 剑指 Offer 29. 顺时针打印矩阵
 * @date 2023/03/02
 **/
public class ShunShiZhenDaYinJuZhenLcof {

    public static void main(String[] args) {
        int[][] matrix = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12}
        };

        int[] a = {1, 2, 3, 4};
        int[] b = new int[]{4, 5, 6, 7};

        // [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
        Solution solution = new Solution();
        int[] res = solution.spiralOrder(matrix);
        for (int num : res) {
            System.out.print(num + ";");
        }
    }

    public static class Solution {
        public int[] spiralOrder(int[][] matrix) {
            // 右遍历
            // 下遍历
            // 左遍历
            // 上遍历
            if (matrix == null) {
                return new int[0];
            }
            if (matrix.length == 0 || matrix[0].length == 0) {
                return new int[0];
            }
            Set<String> visitPoint = new HashSet();
            boolean[][] visited = new boolean[matrix.length][matrix[0].length];
            int totalSize = matrix.length * matrix[0].length;
            int i = 0, j = 0, index = 0, visitCnt = 0;
            int[] result = new int[totalSize];
            while (visitCnt != totalSize) {
                System.out.println("新一轮遍历开始");
                System.out.println("开始右遍历");
                // 右遍历
                System.out.println("visit i = " + i + ";j = " + j);

                while (j < matrix[0].length && !visited[i][j]) {
                    System.out.println("右遍历 visit i = " + i + ";j = " + j);
                    visited[i][j] = true;
                    visitCnt++;
                    result[index++] = matrix[i][j++];
                }
                j--;
                i++;
                System.out.println("开始下遍历");
                // 下遍历
                while (i < matrix.length && !visited[i][j]) {
                    System.out.println("下遍历 visit i = " + i + ";j = " + j);
                    visited[i][j] = true;
                    visitCnt++;

                    result[index++] = matrix[i++][j];
                }
                i--;
                System.out.println("开始左遍历");
                j--;
                // 左遍历
                while (j >= 0 && !visited[i][j]) {
                    System.out.println("左遍历 visit i = " + i + ";j = " + j);
                    visited[i][j] = true;
                    visitCnt++;

                    result[index++] = matrix[i][j--];
                }
                j++;
                i--;
                System.out.println("开始上遍历");

                // 上遍历
                while (i >= 0 && !visited[i][j]) {
                    System.out.println("上遍历 visit i = " + i + ";j = " + j);
                    visited[i][j] = true;
                    visitCnt++;
                    result[index++] = matrix[i--][j];
                }
                i++;
                j++;
            }
            return result;
        }
    }
}
